package com.zl.autocode.controller.generator;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zl.autocode.common.constant.AutocodeConfigC;
import com.zl.autocode.controller.base.AutocodeBaseController;
import com.zl.autocode.dto.generator.ReqGeneratorSelectDictDTO;
import com.zl.autocode.entity.autocode.ZLCfgGenerate;
import com.zl.autocode.entity.autocode.ZlDdlTemplate;
import com.zl.autocode.entity.autocode.ZlDict;
import com.zl.autocode.entity.autocode.ZlDictDtl;
import com.zl.autocode.entity.autocode.ZlFieldMapping;
import com.zl.autocode.entity.autocode.ZlPreviewTemplate;
import com.zl.autocode.entity.autocode.ZlProject;
import com.zl.autocode.entity.autocode.ZlProjectModule;
import com.zl.autocode.entity.autocode.ZlTable;
import com.zl.autocode.entity.autocode.ZlTableColumn;
import com.zl.autocode.entity.autocode.ZlTemplate;
import com.zl.autocode.entity.autocode.ZlTemplateDtl;
import com.zl.autocode.entity.autocode.ZlValidation;
import com.zl.autocode.entity.autocode.ZlWebComponent;
import com.zl.autocode.service.generator.IGeneratorSelectService;
import com.zl.common.aop.LogSimpleResult;
import com.zl.common.dto.ReqDTO;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *  下拉选项，入口统一维护
 *  尽量只返回必须字段
 * @author ZL
 */
@Api(value="下拉选项")
@RestController("autocodeGeneratorSelectController")
@RequestMapping("/autocode/select")
public class GeneratorSelectController extends AutocodeBaseController {

    @Autowired
    private IGeneratorSelectService generatorSelectService;
    
    
    /**
     * 下拉选项-字典,以及其他
     */
    @LogSimpleResult
    @ApiOperation(value="下拉选项")
    @RequestMapping(value="/dict", method = RequestMethod.POST)
    public Object selectDict(@RequestBody ReqDTO<ReqGeneratorSelectDictDTO> reqInfo) throws Exception{
        ReqGeneratorSelectDictDTO reqData = reqInfo.getReqData();
        Map<String,Map<String, Object>> mapAll = new LinkedHashMap<>();
        selectDictHander(mapAll, reqData.getType());
        return successDATA(mapAll);
    }
    private void selectDictHander(Map<String,Map<String, Object>> mapAll, String type) throws Exception{
        //字典明细
        List<ZlDictDtl> dataDictDtl = generatorSelectService.queryDictDtlForSelect(type);
        for(ZlDictDtl lt : dataDictDtl){
            Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
            mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getDictDisplay1());
            if(mapAll.get(lt.getDictType()) != null){
                mapAll.get(lt.getDictType()).put(lt.getDictDtlNo(), lt);
            }else{
                Map<String, Object> map = new LinkedHashMap<>();
                map.put(lt.getDictDtlNo(), lt);
                mapAll.put(lt.getDictType(), map);
            }
        }
    }
    /**
     * 下拉选项
     */
    @LogSimpleResult
    @ApiOperation(value="下拉选项")
    @RequestMapping(value="/dictz", method = RequestMethod.POST)
    public Object selectDictz(@RequestBody ReqDTO<ReqGeneratorSelectDictDTO> reqInfo) throws Exception{
        ReqGeneratorSelectDictDTO reqData = reqInfo.getReqData();
        String type = reqData.getType();
        String key = reqData.getKey();
        String key2 = reqData.getKey2();
        Map<String,Map<String, Object>> mapAll = new LinkedHashMap<>();
        List<String> types = new ArrayList<>();
        if(StringUtils.isNotBlank(type)){
            types = ListUtil.toList(type.split(",")); 
        }
        
        if(types.contains("ZSELECT_DICT")){
            //字典类型
            List<ZlDict> dataDict = generatorSelectService.queryDictForSelect();
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlDict lt : dataDict){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getDictTitle());
                map.put(lt.getDictType(), mapData);
            }
            mapAll.put("ZSELECT_DICT", map);
        }
        if(types.contains("ZSELECT_FIELD_MAPPING")){
            //映射信息
            List<ZlFieldMapping> data = generatorSelectService.queryFieldMappingForSelect();
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlFieldMapping lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getMappingTitle());
                map.put(lt.getMappingId(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_FIELD_MAPPING_"+lt.getDatabaseType());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getMappingId(), mapData);
                mapAll.put("ZSELECT_FIELD_MAPPING_"+lt.getDatabaseType(), mapchild);
            }
            mapAll.put("ZSELECT_FIELD_MAPPING", map);
        }
        if(types.contains("ZSELECT_PROJECT")){
            //项目 
            List<ZlProject> data = generatorSelectService.queryProjectForSelect();
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlProject lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getProjectTitle());
                map.put(lt.getProjectId(), mapData);
            }
            mapAll.put("ZSELECT_PROJECT", map);
        }
        if(types.contains("ZSELECT_PROJECT_MODULE")){
            //项目模块
            List<ZlProjectModule> data = generatorSelectService.queryProjectModuleForSelect(key);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlProjectModule lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getModuleTitle());
                map.put(lt.getModuleId(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_PROJECT_MODULE_"+lt.getProjectId());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getModuleId(), mapData);
                mapAll.put("ZSELECT_PROJECT_MODULE_"+lt.getProjectId(), mapchild);
            }
            mapAll.put("ZSELECT_PROJECT_MODULE", map);
        }
        if(types.contains("ZSELECT_VALIDATION")){
            //校验规则
            List<ZlValidation> data = generatorSelectService.queryValidationForSelect(key);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlValidation lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getValidationTitle());
                map.put(lt.getValidationId(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_VALIDATION_"+lt.getProjectId());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getValidationId(), mapData);
                mapAll.put("ZSELECT_VALIDATION_"+lt.getProjectId(), mapchild);
            }
            mapAll.put("ZSELECT_VALIDATION", map);
        }
        if(types.contains("ZSELECT_TABLE")){
            //表信息
            List<ZlTable> data = generatorSelectService.queryTableForSelect(key);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlTable lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getTableTitle());
                map.put(lt.getTableId(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_TABLE_"+lt.getProjectId());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getTableCode(), mapData);
                mapAll.put("ZSELECT_TABLE_"+lt.getProjectId(), mapchild);
            }
            mapAll.put("ZSELECT_TABLE", map);
        }
        if(types.contains("ZSELECT_TABLE_COLUMN")){
            //表信息
            List<ZlTableColumn> data = generatorSelectService.queryTableColumnForSelect(key,key2);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlTableColumn lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getColumnTitle());
                map.put(lt.getColumnId(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_TABLE_COLUMN_"+lt.getTableCode());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getColumnCode(), mapData);
                mapAll.put("ZSELECT_TABLE_COLUMN_"+lt.getTableCode(), mapchild);
            }
            mapAll.put("ZSELECT_TABLE_COLUMN", map);
        }
        if(types.contains("ZSELECT_TEMPLATE")){
            //模板信息
            List<ZlTemplate> data = generatorSelectService.queryTemplateForSelect(key);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlTemplate lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getTemplateTitle());
                map.put(lt.getTemplateId(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_TEMPLATE_"+lt.getModuleId());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getTemplateId(), mapData);
                mapAll.put("ZSELECT_TEMPLATE_"+lt.getModuleId(), mapchild);
            }
            mapAll.put("ZSELECT_TEMPLATE", map);
        }
        if(types.contains("ZSELECT_TEMPLATE_DTL")){
            //模板信息
            List<ZlTemplateDtl> data = generatorSelectService.queryTemplateDtlForSelect(key);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlTemplateDtl lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getFileTitle());
                map.put(lt.getTemplateId(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_TEMPLATE_DTL_"+lt.getTemplateId());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getTemplateFileId(), mapData);
                mapAll.put("ZSELECT_TEMPLATE_DTL_"+lt.getTemplateId(), mapchild);
            }
            mapAll.put("ZSELECT_TEMPLATE_DTL", map);
        }
        if(types.contains("ZSELECT_WEB_COMPONENT")){
            //前端组件信息
            List<ZlWebComponent> data = generatorSelectService.queryWebComponentForSelect(key);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlWebComponent lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getComponentTitle());
                map.put(lt.getComponentId(), mapData);
            }
            mapAll.put("ZSELECT_WEB_COMPONENT", map);
        }
        if(types.contains("ZSELECT_CFG_GENERATE")){
            //前端组件信息
            List<ZLCfgGenerate> data = generatorSelectService.queryCfgGenerateForSelect(key);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZLCfgGenerate lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getCfgGenerateTitle());
                map.put(lt.getCfgGenerateId(), mapData);
            }
            mapAll.put("ZSELECT_CFG_GENERATE", map);
        }
        if(types.contains("ZSELECT_DDL_TEMPLATE")){
            //ddl模板
            List<ZlDdlTemplate> data = generatorSelectService.queryDdlTemplateForSelect(key);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlDdlTemplate lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getTemplateTitle());
                map.put(lt.getTemplateId(), mapData);
            }
            mapAll.put("ZSELECT_DDL_TEMPLATE", map);
        }
        if(types.contains("ZSELECT_PREVIEW_TEMPLATE")){
            //ddl模板
            List<ZlPreviewTemplate> data = generatorSelectService.queryPreviewTemplateForSelect(key);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlPreviewTemplate lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(AutocodeConfigC.LABEL_VALUE, lt.getTemplateTitle());
                map.put(lt.getTemplateId(), mapData);
            }
            mapAll.put("ZSELECT_PREVIEW_TEMPLATE", map);
        }
        return successDATA(mapAll);
    }
}